package cms.simmytech.web.activity;

import cms.simmytech.model.TbActivity;
import cms.simmytech.model.TbActivityUser;
import cms.simmytech.model.TbContent;
import cms.simmytech.model.TbMajor;
import cms.simmytech.service.ActivityService;
import cms.simmytech.web.base.BaseController;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.model.IModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/activity")
public class ActivityController extends BaseController {
    @Autowired
    public ActivityService activityService;
    //查询并显示活动列表
    @RequestMapping(value = "/activityList")
    public String activityList(@RequestParam(value = "currentPage", defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, String name, Integer collegeId,String userName, Byte activityState,Model model) {

        List<TbActivity> list=activityService.getAll();
        for (TbActivity tb:list) {
            if ((System.currentTimeMillis()/1000-tb.getActivityEnd())<0&&(System.currentTimeMillis()/1000-tb.getActivityStart()>0)){
                tb.setActivityState((byte)2);
            }
            if((System.currentTimeMillis()/1000-tb.getActivityEnd()>0)){
                tb.setActivityState((byte)3);
            }
            if((System.currentTimeMillis()/1000-tb.getActivityStart()<0)){
                tb.setActivityState((byte)0);
            }
            if(System.currentTimeMillis()/1000-tb.getEnrollEnd()<0&&System.currentTimeMillis()/1000-tb.getEnrollStart()>0){
                tb.setActivityState((byte)1);
            }
            activityService.updateActivityById(tb);
        }
        //System.out.println(userName);
        PageInfo<TbActivity> pageInfo = activityService.getPageList(pageNum, pageSize,name,collegeId,userName,activityState);
//        List<String> userNameList=activityService.getUserNameList();//活动发起人的姓名(发起用户名user_name)列表

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("collegeId", collegeId);//在前端页面回显学院名称
        model.addAttribute("name", name);//在前端页面回显搜索的活动主题关键字
//        model.addAttribute("userNameList",userNameList);//在前端页面回显下拉选择框的选项
        model.addAttribute("theSelectedUserName",userName);//theSelectedUserName搜索时选中的用户名称
        model.addAttribute("activityState",activityState);//回显活动状态
        return super.toPage("activity/actvityList", model);
    }
//    @RequestMapping(value="/activityDetail")
//    public String activityDetail(Integer activityId,Model model){//用来显示一个专业的详细信息
//        TbActivity activity=null;
//        if(activityId==null){//当专业id为空时，显示空白详情页，用来新增专业信息
//            activity=new TbActivity();
//        }else{//当专业id非空时，查询专业信息，用来修改专业信息
//            activity=activityService.findActivityById(activityId);
//        }
//        model.addAttribute("activity",activity);
//        return super.toPage("activity/activityDetail",model);//以后返回页面，尽量用return super.toPage()
//    }
    @RequestMapping(value="/activityDetail")
    public String activityDetail(Integer activityId,Model model){//用来显示一个专业的详细信息
        TbActivity activity=null;
        if(activityId==null){//当专业id为空时，显示空白详情页，用来新增专业信息
            activity=new TbActivity();
        }else{//当专业id非空时，查询专业信息，用来修改专业信息
            activity=activityService.findActivityById(activityId);
        }
        List<TbActivityUser> activityUser=activityService.getActivityUserListById(activityId);
        model.addAttribute("activity",activity);
        model.addAttribute("activityUser",activityUser);
        return super.toPage("activity/activityDetail",model);//以后返回页面，尽量用return super.toPage()
    }
    @RequestMapping(value="/activityEdit")
    public String activityEdit(Integer activityId,Model model){//用来显示一个专业的详细信息
        TbActivity activity=null;
        if(activityId!=null){//当专业id为空时，显示空白详情页，用来新增专业信息
            activity=activityService.findActivityById(activityId);
        }
        model.addAttribute("activity",activity);
        return super.toPage("activity/activityEdit",model);//以后返回页面，尽量用return super.toPage()
    }
    @RequestMapping(value="/activityAdd")
    public String activityAdd(Model model){//用来显示一个专业的详细信息
        TbActivity activity=null;
        activity=new TbActivity();
        model.addAttribute("activity",activity);
        return super.toPage("activity/activityAdd",model);//以后返回页面，尽量用return super.toPage()
    }
    //删除活动
    @RequestMapping(value = "/deleteActivity")
    @ResponseBody
    public Map<String, Object> deleteMajorById(Integer activityId) {
        Map<String, Object> map = new HashMap<String, Object>();
        activityService.deleteActivityById(activityId);
        map.put("res", true);
        return map;
    }
}
